<!DOCTYPE html>
<html lang="en">
<head>
    
    <link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
    <link rel="stylesheet" href="/assets/main.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Installing RDKit on Google Colab</title>
    <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 rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan" />
    <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>
    <center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<a href="/3D-Designs/">3D designs</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
    
<main>

	<h1 id="installing-rdkit-on-google-colab">Installing RDKit on Google Colab</h1>

<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">&quot;Miniconda3-latest-Linux-x86_64.sh&quot;</span><span class="p">,</span>
        <span class="n">url_base</span><span class="o">=</span><span class="s2">&quot;https://repo.continuum.io/miniconda/&quot;</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">&quot;~&quot;</span><span class="p">,</span> <span class="s2">&quot;miniconda&quot;</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">&quot;&quot;&quot;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>

	<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>
	<section id="isso-thread">
	    <noscript>Javascript needs to be activated to view comments.</noscript>
	</section>
</main>

    <script src="assets/manup.min.js"></script>
    <script src="/pwabuilder-sw-register.js"></script>    
</body>
</html>