<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#6a9fb5">
<title>Installing RDKit on Google Colab</title>
<!--
<link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
-->
<link rel="stylesheet" href="/assets/c-hyde.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700">
<link rel="stylesheet" href="/assets/main.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="og:site_name" content="Navan Chauhan">
<link rel="canonical" href="https://web.navan.dev/posts/2020-07-01-Install-rdkit-colab.html">
<meta name="twitter:url" content="https://web.navan.dev/posts/2020-07-01-Install-rdkit-colab.html">
<meta name="og:url" content="https://web.navan.dev/posts/2020-07-01-Install-rdkit-colab.html">
<meta name="twitter:title" content="Installing RDKit on Google Colab">
<meta name="og:title" content="Installing RDKit on Google Colab">
<meta name="description" content="Install RDKit on Google Colab with one code snippet.">
<meta name="twitter:description" content="Install RDKit on Google Colab with one code snippet.">
<meta name="og:description" content="Install RDKit on Google Colab with one code snippet.">
<meta name="twitter:card" content="summary_large_image">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/images/favicon.png" type="image/png">
<link href="/feed.rss" type="application/atom+xml" rel="alternate" title="Sitewide Atom feed">
<meta name="twitter:image" content="https://web.navan.dev/images/opengraph/posts/2020-07-01-Install-rdkit-colab.png">
<meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2020-07-01-Install-rdkit-colab.png">
<meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k">
<script data-goatcounter="https://navanchauhan.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
<script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
<link rel="manifest" href="/manifest.json">
</head>
<body class="theme-base-0d">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<h1><a href="/">Navan</a></h1>
<p class="lead" id="random-lead">Alea iacta est.</p>
</div>
<ul class="sidebar-nav">
<li><a class="sidebar-nav-item" href="/about/">about/links</a></li>
<li><a class="sidebar-nav-item" href="/posts/">posts</a></li>
<li><a class="sidebar-nav-item" href="/3D-Designs/">3D designs</a></li>
<li><a class="sidebar-nav-item" href="/feed.rss">RSS Feed</a></li>
<li><a class="sidebar-nav-item" href="/colophon/">colophon</a></li>
</ul>
<div class="copyright"><p>© 2019-2024. Navan Chauhan <br> <a href="/feed.rss">RSS</a></p></div>
</div>
</div>
<script>
let phrases = [
"Something Funny", "Veni, vidi, vici", "Alea iacta est", "In vino veritas", "Acta, non verba", "Castigat ridendo mores",
"Cui bono?", "Memento vivere", "अहम् ब्रह्मास्मि", "अनुगच्छतु प्रवाहं", "चरन्मार्गान्विजानाति", "coq de cheval", "我愛啤酒"
];
let new_phrase = phrases[Math.floor(Math.random()*phrases.length)];
let lead = document.getElementById("random-lead");
lead.innerText = new_phrase;
</script>
<div class="content container">
<div class="post">
<h1 id="installing-rdkit-on-google-colab">Installing RDKit on Google Colab</h1>
<p><strong>Update: March 2024</strong> </p>
<p><code>rdkit-pypi</code> has been deprecated in favour of <code>rdkit</code></p>
<p>You can simply run:</p>
<pre><code>!pip install rdkit
</code></pre>
<p><strong>EDIT:</strong> Try installing RDKit using pip</p>
<pre><code>!pip install rdkit-pypi
</code></pre>
<h2 id="old-method-still-works">Old Method (Still Works)</h2>
<p>RDKit is one of the most integral part of any Cheminfomatic specialist's toolkit but it is notoriously difficult to install unless you already have <code>conda</code> installed. I originally found this in a GitHub Gist but I have not been able to find that gist again :/ </p>
<p>Just copy and paste this in a Colab cell and it will install it 👍</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">getLogger</span><span class="p">,</span> <span class="n">StreamHandler</span><span class="p">,</span> <span class="n">INFO</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">StreamHandler</span><span class="p">())</span>
<span class="n">logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">INFO</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">install</span><span class="p">(</span>
<span class="n">chunk_size</span><span class="o">=</span><span class="mi">4096</span><span class="p">,</span>
<span class="n">file_name</span><span class="o">=</span><span class="s2">"Miniconda3-latest-Linux-x86_64.sh"</span><span class="p">,</span>
<span class="n">url_base</span><span class="o">=</span><span class="s2">"https://repo.continuum.io/miniconda/"</span><span class="p">,</span>
<span class="n">conda_path</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"~"</span><span class="p">,</span> <span class="s2">"miniconda"</span><span class="p">)),</span>
<span class="n">rdkit_version</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">add_python_path</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">force</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="s2">"""install rdkit from miniconda</span>
<span class="s2"> </span>
</code></pre>
</div>
<pre><code>import rdkit_installer
rdkit_installer.install()
```
"""
python_path = os.path.join(
conda_path,
"lib",
"python{0}.{1}".format(*sys.version_info),
"site-packages",
)
if add_python_path and python_path not in sys.path:
logger.info("add {} to PYTHONPATH".format(python_path))
sys.path.append(python_path)
if os.path.isdir(os.path.join(python_path, "rdkit")):
logger.info("rdkit is already installed")
if not force:
return
logger.info("force re-install")
url = url_base + file_name
python_version = "{0}.{1}.{2}".format(*sys.version_info)
logger.info("python version: {}".format(python_version))
if os.path.isdir(conda_path):
logger.warning("remove current miniconda")
shutil.rmtree(conda_path)
elif os.path.isfile(conda_path):
logger.warning("remove {}".format(conda_path))
os.remove(conda_path)
logger.info('fetching installer from {}'.format(url))
res = requests.get(url, stream=True)
res.raise_for_status()
with open(file_name, 'wb') as f:
for chunk in res.iter_content(chunk_size):
f.write(chunk)
logger.info('done')
logger.info('installing miniconda to {}'.format(conda_path))
subprocess.check_call(["bash", file_name, "-b", "-p", conda_path])
logger.info('done')
logger.info("installing rdkit")
subprocess.check_call([
os.path.join(conda_path, "bin", "conda"),
"install",
"--yes",
"-c", "rdkit",
"python=={}".format(python_version),
"rdkit" if rdkit_version is None else "rdkit=={}".format(rdkit_version)])
logger.info("done")
import rdkit
logger.info("rdkit-{} installation finished!".format(rdkit.__version__))
</code></pre>
<p>if <strong>name</strong> == "<strong>main</strong>":
install()
```</p>
</div>
<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
<script data-isso="https://comments.navan.dev/"
src="https://comments.navan.dev/js/embed.min.js"></script>
<div id="isso-thread">
<noscript>Javascript needs to be activated to view comments.</noscript>
</div>
</div>
<script src="assets/manup.min.js"></script>
<script src="/pwabuilder-sw-register.js"></script>
</body>
</html>